//
//  AppDelegate.m
//  laowu
//
//  Created by yinqy on 15/4/23.
//  Copyright (c) 2015年 长沙市岳麓区立早网络科技有限公司. All rights reserved.
//
//劳务的 APP 账号
//Apple Id:1662733760@qq.com
//Password:Fcy1662733760
//接口地址：http://lwpt.mydoc.io/?v=3589
//后台址址：http://180.97.80.245:8787/
//后台地址：http://180.97.80.245:8787/admin/cms/index.do
//后台帐号密码：admin password
//测试帐号密码：18673153265 211000184q
//劳务的百度统计 AppKey:c36196e7c4
//百度地图定位 AppKey:PdQwG0zbu0m8pPVmDcbVIBIR
//百度云推送的：
//API Key： 6v9nRsxhyWRY9mUsQZLYiGnY
//Secret Key：ihqO149RXMLLeyGpcQlIkA1MlZa1Iqio


#import "AppDelegate.h"
#import "CustomTabBarCtrl.h"
#import "BPush.h"
#import "HomeDataTool.h"
#import "BaiduMobStat.h"

@interface AppDelegate ()<BPushDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    //计算缩放比值
    AppDelegate *myDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    if(SCREENH > 480){
        myDelegate.autoSizeScaleX = SCREENW/320;
        myDelegate.autoSizeScaleY = SCREENH/568;
    }else{
        myDelegate.autoSizeScaleX = 1.0;
        myDelegate.autoSizeScaleY = SCREENH/568;
    }
    
    //设置整体的导航栏的背影颜色
    [[UINavigationBar appearance] setBarTintColor:THEME_COLOR];
    
    self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    [self.window setRootViewController:[[CustomTabBarCtrl alloc] init]];
    [self.window makeKeyAndVisible];
    
    //初始化百度定位相关
    [self initBaiduMapLocation];
    
    [self initBaiduPushWithOptions:launchOptions];
    [self initBaiduMobStat];
    return YES;
}


-(void)initBaiduMobStat{
    BaiduMobStat *state = [BaiduMobStat defaultStat];
    [state startWithAppId:@"c36196e7c4"];
}

-(void)initBaiduPushWithOptions:(NSDictionary *)launchOptions{
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0) {
        UIUserNotificationType myTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound
        | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:myTypes
                                                                                 categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }else {
        UIRemoteNotificationType myTypes = UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound;
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:myTypes];
    }
    [BPush setDelegate:self];
    // 在 App 启动时注册百度云推送服务,需要提供 Apikey
    [BPush registerChannel:launchOptions apiKey:@"FOrD9R6eyihE2sHUnAw273Ru" pushMode:BPushModeProduction isDebug:NO];
    // 设置 BPush 的回调 [BPush setDelegate:self];
    // App 是⽤用户点击推送消息启动
    NSDictionary *userInfo = [launchOptions objectForKey:UIApplicationLaunchOptionsRemoteNotificationKey];
    if (userInfo) {
        [BPush handleNotification:userInfo];
    }
   
}

-(void)initBaiduMapLocation{
    _mapManager = [[BMKMapManager alloc]init];
    // 如果要关注网络及授权验证事件，请设定 generalDelegate参数
    BOOL ret = [_mapManager start:@"FOrD9R6eyihE2sHUnAw273Ru"  generalDelegate:nil];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}

- (void)onMethod:(NSString*)method response:(NSDictionary*)data{
    if ([method isEqualToString:BPushRequestMethodBind] && ISLOGIN) {
        [self pushBind];
    }
}

-(void)pushBind{
    [HomeDataTool pushBindWithChannelId:[BPush getChannelId] bUserId:[BPush getChannelId] success:^(BaseResponse *result) {
        
    } failure:^(NSError *error) {
        
    }];
}

-(void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings{
    [application registerForRemoteNotifications];
}


- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [BPush registerDeviceToken:deviceToken];
    [BPush bindChannel];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    // App 收到推送通知
    [BPush handleNotification:userInfo];
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
